﻿using B.S.BasicData.Api.Write.Application.Command.Account;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.Handler.Account
{
    public class UpadateAccountImgHandler:IRequestHandler<UpadateAccountImgCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;

        public UpadateAccountImgHandler(IBaseRepository<AccountModel> accountRepo)
        {
            this.accountRepo = accountRepo;
        }

        public async Task<ApiResult<int>> Handle(UpadateAccountImgCommand request, CancellationToken cancellationToken)
        {
          

            var accountEntity = await accountRepo.GetModel(request.Id); //根据id查找特定的一个账号对象

            if (accountEntity == null) { 
             return new ApiResult<int> {  Code=ApiEnum.Failed,Msg="未找到要修改的账号数据",Data=0};
            }

            accountEntity.Img = request.Img;
            
            var r = await accountRepo.UpdateAsync(accountEntity);

            if (r <= 0)
            {
                return new ApiResult<int> { Code = ApiEnum.Failed, Msg = "修改失败", Data = r };
            }
            else {
                return new ApiResult<int> { Code = ApiEnum.Success, Msg = "修改成功", Data = r };

            }
             
        }
    }
}
